//
//  MJTWelcomeViewController.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/12.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SDWebImage
class MJTWelcomeViewController: UIViewController {

    //懒加载控件
    //背景图片加载
    private lazy var bgimageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    //头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //判断大小
        if let imageUrl = MJTUserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
            imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //提示信息
    private lazy var messageLabel: UILabel = {
        let myLabel = UILabel()
        myLabel.font = UIFont.systemFontOfSize(15)
        myLabel.textColor = UIColor.grayColor()
        if let name = MJTUserAccountViewModel.shareUserAccount.userAccount?.name {
            myLabel.text = "欢迎回来,\(name)"
        } else {
            myLabel.text = "欢迎回来"
        }
        myLabel.alpha = 0
        return myLabel
    }()
    override func loadView() {
        view = bgimageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //执行动画
        startAnimation()
    }
    func setUpUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)

        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(260)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    }
    //开启动画
    private func startAnimation() {
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            //执行约束动画
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(2, animations: { () -> Void in
                    //设置透明度显示
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //进入首页 利用通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)

                })
        }
    }

}
